﻿namespace Sidvall.CodeGenerator
{
	public class DomainCommandCollection : System.Collections.ObjectModel.KeyedCollection<string, DomainCommand>
	{
		#region Business Members

		#region Add

		public DomainCommand Add(string name, string resultTypeName)
		{
			DomainCommand item;

			item = new DomainCommand()
			{
				Name = name,
				ResultTypeName = resultTypeName
			};
			Add(item);

			return item;
		}

		#endregion
		#region GetKeyForItem

		protected override string GetKeyForItem(DomainCommand item)
		{
			if (item == null)
				throw new System.ArgumentNullException("item");

			return item.Name;
		}

		#endregion
		#region Import

		public void Import(System.Collections.Generic.IEnumerable<DomainCommand> items)
		{
			if (items == null)
				return;

			foreach (var item in items)
			{
				if (Contains(item.Name))
					this[item.Name].Import(item);
				else
					Add(item);
			}
		}

		#endregion
		#region InsertItem

		protected override void InsertItem(int index, DomainCommand item)
		{
			if ((item != null) && (this.DomainModel != null))
				item.DomainModel = this.DomainModel;
			base.InsertItem(index, item);
		}

		#endregion
		#region SetItem

		protected override void SetItem(int index, DomainCommand item)
		{
			if ((item != null) && (this.DomainModel != null))
				item.DomainModel = this.DomainModel;
			base.InsertItem(index, item);
		}

		#endregion

		#endregion
		#region Helper Members

		#region DomainModel

		private DomainModel _DomainModel;
		private DomainModel DomainModel
		{
			get
			{
				return _DomainModel;
			}
			set
			{
				_DomainModel = value;
				foreach (var item in this)
					item.DomainModel = value;
			}
		}

		#endregion

		#region SetDomainModel

		internal void SetDomainModel(DomainModel item)
		{
			this.DomainModel = item;
		}

		#endregion

		#endregion
		#region Constructors

		public DomainCommandCollection()
			: base(System.StringComparer.OrdinalIgnoreCase)
		{
		}

		#endregion
	}
}
